/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package klient;

import Serializable.ParsedCmd;
import graphic.Graphic;
import graphic.Loop;

/**
 *
 * @author nicra
 */
public class Protocol {
    
    public static boolean needAny(UserState uS){
        if(uS.needLogin() || uS.needPass()) return true;
        if(Bridge.needAny) return true;
        return false;        
    }
    
    public static ParsedCmd makeReq(UserState uS){
        String cmd="";
        String arg="";
         if(Bridge.needAny) {
             Bridge.setNeedAny(false);
             return Bridge.cmd;
             
         }
        
        if(uS.wantReg) cmd+="R";
        if(uS.needLogin()) {
            cmd+="LOGIN";
            arg=uS.nick;
        }else if(uS.needPass()){
            cmd+="PASS";
            arg=uS.pass;
        }
        if(cmd.isEmpty() || "R".equals(cmd)) return new ParsedCmd("ALLOK");
        return new ParsedCmd(cmd,arg);
    }
    
    public static ParsedCmd process(ParsedCmd pC,UserState uS){
        ParsedCmd out;
        out=new ParsedCmd(); 
        if(pC.mainCmd.startsWith("X")){
            Bridge.toDo(pC);
            return out;
        }
        switch(pC.mainCmd){
            case "HAI":
                uS.loginState=UserState.JUST_CONNECTED;
                out=new ParsedCmd("ID PLZ").needAnswer();
                break;
            case "UID":
                uS.ID=pC.intParam[0];
                uS.loginState=UserState.HAS_ID;
                out=new ParsedCmd();
                break;
            case "LOGINOK":
                uS.loginState=UserState.IS_LOGIN;
                out=new ParsedCmd();
                break;
            case "PASSOK":
                uS.loginState = UserState.IS_PASS;
                uS.wantReg = false;
                out=new ParsedCmd();
                Bridge.setErrLabel("OK");
                Klient.frame.setVisible(true);
                Bridge.setSucces(true);
                Bridge.setOpPending(false);
                break;
            case "BAD":
            case "PASSBAD":
            case "LOGINBAD":
                if(uS.wantReg)
                    Bridge.setErrLabel("Login zajety lub haslo nieprawidłowe.");
                else
                    Bridge.setErrLabel("Zly login lub haslo!");
                uS.loginErr();
                out=new ParsedCmd("MYBAD");
                Bridge.setSucces(false);
                Bridge.setOpPending(false);
                break;
            case "More?":
            case "UOK?":
                out=makeReq(uS).needAnswer();
                break; 
            case "CONNECTED":
                Bridge.setConnected(true);
                break;
                
            case "PLOADED":
                Bridge.loaded[pC.intParam[0]]=true;
                break;
            case "PLAYGAME":
                Bridge.readyToStart=true;
                break;
            case "PLIST":
                for(int i=0;i<pC.stringParam.length;++i){
                    Bridge.players[i]=pC.stringParam[i];
                    Bridge.ready[i]=pC.intParam[i]==1;
                }
                break;
            case "NPLAYER":
                Bridge.players[pC.intParam[0]]=pC.stringParam[0];
                Bridge.ready[pC.intParam[0]]=false;
                if(pC.stringParam[0].equals(Klient.uS.nick)){
                    Klient.uS.IDinGame=pC.intParam[0];
                }
                Bridge.maxPlayers=pC.intParam[1];
                break;
            case "DCPLAYER":
                Bridge.players[pC.intParam[0]]="Czekaj...";
                Bridge.ready[pC.intParam[0]]=false;
                break;
            case "RDPLAYER":
                Bridge.ready[pC.intParam[0]]=true;
                break;
            case "NRDPLAYER":
                Bridge.ready[pC.intParam[0]]=false;
                break;
            case "STARTGAME":
                Bridge.startGame=true;
                break;
            case "SLIST":
                Bridge.setRet(pC);
                Bridge.setOpPending(false);
                break;
            case "OK":
                Bridge.setSucces(true);
                Bridge.setOpPending(false);
                break;
            case "NOPE":
                    Bridge.setSucces(false);
                    Bridge.setOpPending(false);
                break;
            default:
                out=new ParsedCmd();             
        }
        return out;
    }
}
